#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#

from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence
from base.ChoicesModifier import ChoicesModifier
from base.TestException import *
from gpgpu.AssemblyHelperGPGPU import AssemblyHelperGPGPU
from gpgpu.Utils import LoadGPR64
from base.InstructionMap import InstructionMap
from DV.gpgpu.trees.instruction_tree import *
from DV.gpgpu.trees.csr_trees import *

import RandomUtils

#  --------------------------------------------------------------------------------------------
#  generate random instructions from RV32I instr tree...
#  --------------------------------------------------------------------------------------------

my_RV32I_instructions = {
    "ADD##CALM": 10,
    "ADDI##CALM": 10,
    "AND##CALM": 10,
    "ANDI##CALM": 10,
    "OR##CALM": 10,
    "ORI##CALM": 10,
    "XOR##CALM": 10,
    "XORI##CALM": 10,
}


class MainSequence(Sequence):
    def generate(self, **kargs):
        # pick up a couple of scratch registers...

        (gpr1, gpr2) = self.getRandomRegisters(2, "GPR")

        # read misa, mstatus...

        if self.getPEstate("PrivilegeLevel") == 3:
            self.genInstruction(
                "CSRRS#register#CALM",
                {"rd": gpr1, "rs1": 0, "csr": self.getRegisterIndex("misa")},
            )
            self.genInstruction(
                "CSRRS#register#CALM",
                {
                    "rd": gpr2,
                    "rs1": 0,
                    "csr": self.getRegisterIndex("mstatus"),
                },
            )

        # generate sequences of random instructions...

        self.genRandomInstrs(RV32I_instructions, 10, 50)

    # generate some random instructions...

    def genRandomInstrs(self, instr_tree, min_cnt, max_cnt):
        for _ in range(RandomUtils.random32(min_cnt, max_cnt)):
            the_instruction = self.pickWeighted(instr_tree)
            self.genInstruction(the_instruction)


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
